/**
 *插入[安装申请]时，将选中的[配置清单]里的记录值复制到[安装申请详情]里
 *created by Harry 2017-01-09 21:11:40
 */
public without sharing class AfterInsertInstallationApplyHandler implements Triggers.handler{
    
    public void handle(){
    List<InstallationApply__c> triggerNew = (List<InstallationApply__c>)Trigger.new;
    Set<Id> iAIdSet = new Set<Id>();
    Set<Id> cLIdSet = new Set<Id>();//产品
        
    Map<Id,Id> idMap = new Map<Id,Id>();
    for(InstallationApply__c ia : triggerNew){
    iAIdSet.add(ia.Id);
    cLIdSet.add(ia.Instrument__c);
    idMap.put(ia.Id, ia.Instrument__c);
    }
    Map<Id,InstallationApply__c> filterMap = null;
    Map<Id,Product__c> filterMap2 = null;
    //filterMap为查询出来的“更新后的”[安装申请]对象集
    if(iAIdSet.size()>0){
    filterMap = new Map<Id,InstallationApply__c>([
    SELECT Id,
    (SELECT Name,Remark__c,IsStandard__c,Amount__c,Configuration__c,OrderMethod__c FROM InstallApproval__r)
    FROM InstallationApply__c WHERE Id IN : iAIdSet]);
    }
    //filterMap2为查询出来的[配置清单]对象集
    if(cLIdSet.size()>0){
    filterMap2 = new Map<Id,Product__c>([
    SELECT Id,
    (SELECT Name,IsStandard__c,Amount__c,Remark__c,Configuration__c,OrderMethod__c FROM gSxO__r)
    FROM Product__c WHERE Id IN : cLIdSet]);
    }
    if(null!=filterMap){
    populateDetail(filterMap,filterMap2,idMap);
    }

    }
    
     private void populateDetail(Map<Id,InstallationApply__c> filterMap,Map<Id,Product__c> filterMap2,Map<Id,Id> idMap){
    //待插入的安装申请队列
    List<InstallApprovalDetail__c> preInsertIADList = new List<InstallApprovalDetail__c>();
    for(Id id : filterMap.keySet()){
    InstallationApply__c ia = filterMap.get(id);
    Product__c CL = filterMap2.get(idMap.get(id));
    if(null!=CL){
    List<ConfigurationListDetail__c> CLDList = CL.gSxO__r;
    for(ConfigurationListDetail__c CLD : CLDList){
    InstallApprovalDetail__c newIA = new InstallApprovalDetail__c();
    newIA.Remark__c = CLD.Remark__c;
    newIA.IsStandard__c = CLD.IsStandard__c;
    newIA.Amount__c = CLD.Amount__c;
    newIA.Name = CLD.Name;
    newIA.InstallApproval__c = id;
    newIA.Configuration__c = CLD.Configuration__c;
    newIA.OrderMethod__c = CLD.OrderMethod__c;
    newIA.FromConfiguration__c = true;
    preInsertIADList.add(newIA);
    }
         System.debug( '产品配置明细' + CLDList);
    }

    }
     System.debug(preInsertIADList);
    insert preInsertIADList;
    }
    
    /*public void handle(){
    List<InstallationApply__c> triggerNew = (List<InstallationApply__c>)Trigger.new;
    Set<Id> iAIdSet = new Set<Id>();
    Set<Id> cLIdSet = new Set<Id>();
    Map<Id,Id> idMap = new Map<Id,Id>();
    for(InstallationApply__c ia : triggerNew){
    iAIdSet.add(ia.Id);
    cLIdSet.add(ia.ConfigurationList__c);
    idMap.put(ia.Id, ia.ConfigurationList__c);
    }
    Map<Id,InstallationApply__c> filterMap = null;
    Map<Id,ConfigurationList__c> filterMap2 = null;
    //filterMap为查询出来的“更新后的”[安装申请]对象集
    if(iAIdSet.size()>0){
    filterMap = new Map<Id,InstallationApply__c>([
    SELECT Id,
    (SELECT Name,Remark__c,ShipNo__c,IsStandard__c,Amount__c FROM InstallApproval__r)
    FROM InstallationApply__c WHERE Id IN : iAIdSet]);
    }
    //filterMap2为查询出来的[配置清单]对象集
    if(cLIdSet.size()>0){
    filterMap2 = new Map<Id,ConfigurationList__c>([
    SELECT Id,
    (SELECT Name,IsStandard__c,Amount__c,ShipNo__c,Remark__c FROM ConfigurationListMD__r)
    FROM ConfigurationList__c WHERE Id IN : cLIdSet]);
    }
    if(null!=filterMap){
    populateDetail(filterMap,filterMap2,idMap);
    }

    }
    
    */
    /**
赋值
     */
    /*
    private void populateDetail(Map<Id,InstallationApply__c> filterMap,Map<Id,ConfigurationList__c> filterMap2,Map<Id,Id> idMap){
    //待插入的安装申请队列
    List<InstallApprovalDetail__c> preInsertIADList = new List<InstallApprovalDetail__c>();
    for(Id id : filterMap.keySet()){
    InstallationApply__c ia = filterMap.get(id);
    ConfigurationList__c CL = filterMap2.get(idMap.get(id));
    if(null!=CL){
    List<ConfigurationListDetail__c> CLDList = CL.ConfigurationListMD__r;
    for(ConfigurationListDetail__c CLD : CLDList){
    InstallApprovalDetail__c newIA = new InstallApprovalDetail__c();
    newIA.ShipNo__c = CLD.ShipNo__c;
    newIA.Remark__c = CLD.Remark__c;
    newIA.IsStandard__c = CLD.IsStandard__c;
    newIA.Amount__c = CLD.Amount__c;
    newIA.Name = CLD.Name;
    newIA.InstallApproval__c = id;
    preInsertIADList.add(newIA);
    }
    }
    
    }
    insert preInsertIADList;
    }*/
}